package com.ctrl.riveradministrator.mvp.presenter;

import android.util.Log;

import com.ctrl.riveradministrator.base.BaseObserver;
import com.ctrl.riveradministrator.base.BasePresenter;
import com.ctrl.riveradministrator.RaApplication;
import com.ctrl.riveradministrator.http.UploadRequestBody;
import com.ctrl.riveradministrator.mvp.model.Problem;
import com.ctrl.riveradministrator.mvp.view.activity.problem_report.ProblemReportView;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;

public class ProblemReportPresenter extends BasePresenter<ProblemReportView> {

    public ProblemReportPresenter(ProblemReportView baseView) {
        super(baseView);
    }

    public void assignment(String userId,String problemId) {
        Map<String, String> map = new HashMap<>();
        map.put("distributivePersonBy", userId);
        map.put("problemId", problemId);
        attachView(apiService.assignment(map), new BaseObserver<Problem>(getBaseView()) {
            @Override
            public void onSuccess(Problem model) {
                getBaseView().showMessage();
            }

            @Override
            public void onSuccess(List<Problem> model) {
                getBaseView().showMessage(model);
            }

            @Override
            public void onFailed(String msg) {
                Log.d("onFailed", ":" + msg);
            }

        });
    }


    public void getPerson() {
        Map<String, String> map = new HashMap<>();
        map.put("userId", Objects.requireNonNull(RaApplication.getUserId()));
        attachView(apiService.getCheckPerson(map), new BaseObserver<Problem>(getBaseView()) {
            @Override
            public void onSuccess(Problem model) {
                Log.d("onSuccess", ":");
            }

            @Override
            public void onSuccess(List<Problem> model) {
                getBaseView().showMessage(model);
            }

            @Override
            public void onFailed(String msg) {
                Log.d("onFailed", ":" + msg);
            }

        });
    }

    public void report(List<String> images, String content, String taskId, String personId) {

        for (int i = 0; i < images.size(); i++) {
            File file = new File(images.get(i));
            RequestBody requestFile =
                    RequestBody.create(MediaType.parse("multipart/form-data"), file);
            UploadRequestBody fileRequestBody = new UploadRequestBody(requestFile, (currentLength, contentLength) -> {
                //获取上传的比例
                getBaseView().uploading(currentLength + "/" + contentLength);
            });
            //files是与服务器对应的key
            MultipartBody.Part body =
                    MultipartBody.Part.createFormData("files", file.getName(), fileRequestBody);

            Map<String, RequestBody> params = new HashMap<>();
            params.put("userId", RequestBody.create(MediaType.parse("text/plain"), RaApplication.getUserId()));
            params.put("neirong", RequestBody.create(MediaType.parse("text/plain"), content));
            params.put("renwuId", RequestBody.create(MediaType.parse("text/plain"), taskId));
            params.put("allotUserId", RequestBody.create(MediaType.parse("text/plain"), personId));
            attachView(apiService.problemRecording(
                    params,
                    body), new BaseObserver(getBaseView()) {
                @Override
                public void onSuccess(Object model) {
                    getBaseView().showMessage();
                }

                @Override
                public void onSuccess(List model) {
                    getBaseView().showMessage();
                }

                @Override
                public void onFailed(String msg) {
                    getBaseView().showError(msg);
                }
            });
        }
    }

    public void feedback(List<String> images, String id, String disposeDescribe) {
        for (int i = 0; i < images.size(); i++) {
            File file = new File(images.get(i));
            RequestBody requestFile =
                    RequestBody.create(MediaType.parse("multipart/form-data"), file);
            UploadRequestBody fileRequestBody = new UploadRequestBody(requestFile, (currentLength, contentLength) -> {
                //获取上传的比例
                getBaseView().uploading(currentLength + "/" + contentLength);
            });
            //files是与服务器对应的key
            MultipartBody.Part body =
                    MultipartBody.Part.createFormData("files", file.getName(), fileRequestBody);

            Map<String, RequestBody> map = new HashMap<>();
            map.put("problemId", RequestBody.create(MediaType.parse("text/plain"), id));
            map.put("disposeDescribe", RequestBody.create(MediaType.parse("text/plain"), disposeDescribe));
            attachView(apiService.feedback(map, body), new BaseObserver<Problem>(getBaseView()) {
                @Override
                public void onSuccess(Problem model) {
                    getBaseView().showMessage(null);
                    Log.d("onSuccess", ":");
                }

                @Override
                public void onSuccess(List<Problem> model) {
                    getBaseView().showMessage(model);
                }

                @Override
                public void onFailed(String msg) {
                    Log.d("onFailed", ":" + msg);
                }

            });
        }

    }
}
